import { _decorator, Component, Vec3, RigidBody2D, v2, Animation, Collider2D, Contact2DType, IPhysics2DContact, Node } from 'cc';
import { BirdController } from './BirdController';
import { Bullet } from './Bullet';
import { GameManager } from './GameManager';

const { ccclass, property } = _decorator;

enum ArrowAim{
    animy01,
    animy02,
    animy03,
    animy04,
    animy05,
    animy06

}
@ccclass('ArrowController')
export class ArrowController extends Component {
    private direction: Vec3 = new Vec3();
    private speed: number = 500;
    private isHit = false; // 是否被击中
    private rb: RigidBody2D | null = null;
    ani:Animation;

    arrowAim:ArrowAim;

    @property(GameManager)
    gameManager: GameManager = null;
    
    start() {
        this.ani = this.getComponent(Animation);
        this.rb = this.getComponent(RigidBody2D);

        const collider = this.getComponent(Collider2D);
        collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);

        const animNum = Math.floor(Math.random() * 6);

        if(animNum ==0){
             this.arrowAim = ArrowAim.animy01;
             this.ani.play('animy01')

        }else if(animNum ==1){
            this.arrowAim = ArrowAim.animy02;
            this.ani.play('animy02')
           

        }else if(animNum ==2){
            this.arrowAim = ArrowAim.animy03;
            this.ani.play('animy03')

        }else if(animNum ==3){
            this.arrowAim = ArrowAim.animy04;
            this.ani.play('animy04')

        }else if(animNum ==4){
            this.arrowAim = ArrowAim.animy05;
            this.ani.play('animy05')

        }else if(animNum ==5){
            this.arrowAim = ArrowAim.animy06;
            this.ani.play('animy06')

        }
        
        // console.log('弓箭碰撞子弹事件')
    }

    setDirection(dir: Vec3) {
        this.direction = dir.clone();
    }

    update(deltaTime: number) {
        if (this.isHit || !this.direction) return;
        const movement = this.direction.clone().multiplyScalar(this.speed * deltaTime);
        this.node.setWorldPosition(this.node.worldPosition.add(movement));

        if (this.node.position.y < -640 || this.node.position.y>650 || this.node.position.x < -360 ||this.node.position.x >370) {
            this.node.destroy();
        }
    }


     onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact) {
        const other = otherCollider.node;
        // 子弹碰到的是弓箭，弓箭向右侧随机方向弹走
        if (other.name.includes('Bullet')) {
            this.isHit =true;
            // console.log('gameManager' +this.gameManager)
            this.gameManager.addScore(1);

            if(this.arrowAim == ArrowAim.animy01){
                var ani = this.ani.play('animy01Die')
            } else if(this.arrowAim == ArrowAim.animy02){
                this.ani.play('animy02Die')
            }else if(this.arrowAim == ArrowAim.animy03){
                this.ani.play('animy03Die')
            }else if(this.arrowAim == ArrowAim.animy04){
                this.ani.play('animy04Die')
            }
            else if(this.arrowAim == ArrowAim.animy05){
                this.ani.play('animy05Die')
            }else if(this.arrowAim == ArrowAim.animy06){
                this.ani.play('animy06Die')
            }
          
             // 碰撞事件中这种方式获取组件绑定的组件或脚本是错误的
            // const bulletCtrl = this.gameManager.getComponent(Bullet);
            // bulletCtrl.changeGravity()
            const bulletCtrl = other.getComponent(Bullet);
            bulletCtrl.changeGravity()
         
            // console.log('aniId'+aniId);
            // const rb =this.node.getComponent(RigidBody2D);
            // rb.gravityScale = 1; // 开启受重力影响
            // rb.linearVelocity = v2(0, 0); // 停止其他方向移动
            // 生成一个向右（x > 0）方向的随机速度
            // const randomY = Math.random() * 10 - 5; // -50 ~ 50
            // const velocity = v2(25, randomY); // 向右 + 上/下 的速度
            // rb.linearVelocity = velocity;
            // // 物理碰撞回调中的逻辑不要直接添加或销毁带刚体的节点，需要晚一会
            // this.scheduleOnce(() => {
            //     // 子弹自身销毁
            //     this.node.destroy();
            // }, 5);
        }
    }
}

